﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GluePathModule
{
    public class BackgroundDrawer
    {
        GluePathUnit gluePathUnit;
        string[] str = new string[2];
        int iCount;
        bool bInitOk;
        public BackgroundDrawer(GluePathGroup continuePathUnitInput)
        {
            gluePathUnit = new GluePathUnit();
            gluePathUnit.Name = continuePathUnitInput.Name;
        }
        public void Init()
        {
            try
            {
                string strBackGroupFileName=Application.StartupPath + @"\GluePathSetting\" + gluePathUnit.Name + ".dxf";
                netDxf.DxfDocument dxfDocument = netDxf.DxfDocument.Load(strBackGroupFileName);
                List<netDxf.Entities.EntityObject> listEntities = dxfDocument.Entities.All.ToList();
                gluePathUnit.listContinueItems.Clear();
                for (int i = 0; i < listEntities.Count; i++)
                {
                    if (listEntities[i].Type == netDxf.Entities.EntityType.Line)
                    {
                        GluePathItemLine newItem = new GluePathItemLine();
                        newItem.Name = "Item" + (i + 1).ToString();
                        netDxf.Entities.Line curItem = (netDxf.Entities.Line)listEntities[i];
                        newItem.Pos1.X = curItem.StartPoint.X;
                        newItem.Pos1.Y = curItem.StartPoint.Y;
                        newItem.Pos2.X = curItem.EndPoint.X;
                        newItem.Pos2.Y = curItem.EndPoint.Y;
                        gluePathUnit.listContinueItems.Add(newItem);
                    }
                    if (listEntities[i].Type == netDxf.Entities.EntityType.Arc)
                    {
                        GluePathItemArc newItem = new GluePathItemArc();
                        newItem.Name = "Item" + (i + 1).ToString();
                        netDxf.Entities.Arc curItem = (netDxf.Entities.Arc)listEntities[i];
                        CaliStartMidEndPt(newItem, curItem);
                        gluePathUnit.listContinueItems.Add(newItem);
                    }
                    if (listEntities[i].Type == netDxf.Entities.EntityType.Circle)
                    {
                        GluePathItemCircle newItem = new GluePathItemCircle();
                        newItem.Name = "Item" + (i + 1).ToString();
                        netDxf.Entities.Circle curItem = (netDxf.Entities.Circle)listEntities[i];
                        CaliStartPtAndNormal(newItem,curItem);
                        gluePathUnit.listContinueItems.Add(newItem);
                    }
                    if (listEntities[i].Type == netDxf.Entities.EntityType.Spline)
                    {
                        GluePathItemSPLine newItem = new GluePathItemSPLine();
                        newItem.Name = "Item" + (i + 1).ToString();
                        netDxf.Entities.Spline MyDxfSPLine = (netDxf.Entities.Spline)listEntities[i];
                        for (int ii = 0; ii < MyDxfSPLine.ControlPoints.Length; ii++)
                        {
                            PointData newPoint = new PointData();
                            newPoint.X = MyDxfSPLine.ControlPoints[ii].X;
                            newPoint.Y = MyDxfSPLine.ControlPoints[ii].Y;
                            newItem.ListPoints.Add(newPoint);
                        }
                        gluePathUnit.listContinueItems.Add(newItem);
                    }
                }
                bInitOk = true;
            }
            catch
            {
                bInitOk = false;
            }
        }
        public void CaliStartMidEndPt(GluePathItemArc newItem, netDxf.Entities.Arc curItem)
        {
            double StartAngleTemp = curItem.StartAngle * Math.PI / 180.0;
            double EndAngleTemp = curItem.EndAngle * Math.PI / 180.0;
            double MidAngleTemp = ((EndAngleTemp + StartAngleTemp)) / 2.0;
            if (curItem.Normal.IsNormalized==false)
            {
                newItem.PosStart.X = curItem.Center.X + curItem.Radius * Math.Cos(StartAngleTemp);
                newItem.PosStart.Y = curItem.Center.Y + curItem.Radius * Math.Sin(StartAngleTemp);
                if (EndAngleTemp - StartAngleTemp < 0)
                {
                    MidAngleTemp = StartAngleTemp + ((EndAngleTemp + 2 * Math.PI) - StartAngleTemp) / 2;
                }
                else
                {
                    MidAngleTemp = StartAngleTemp + (EndAngleTemp - StartAngleTemp) / 2;
                }
                newItem.PosMid.X = curItem.Center.X + curItem.Radius * Math.Cos(MidAngleTemp);
                newItem.PosMid.Y = curItem.Center.Y + curItem.Radius * Math.Sin(MidAngleTemp);
                newItem.PosEnd.X = curItem.Center.X + curItem.Radius * Math.Cos(EndAngleTemp);
                newItem.PosEnd.Y = curItem.Center.Y + curItem.Radius * Math.Sin(EndAngleTemp);
                newItem.PosStart.X = -newItem.PosStart.X;
                newItem.PosEnd.X = -newItem.PosEnd.X;
                newItem.PosMid.X = -newItem.PosMid.X;
            }
            else
            {
                newItem.PosStart.X = curItem.Center.X + curItem.Radius * Math.Cos(StartAngleTemp);
                newItem.PosStart.Y = curItem.Center.Y + curItem.Radius * Math.Sin(StartAngleTemp);
                if (EndAngleTemp - StartAngleTemp < 0)
                {
                    MidAngleTemp = StartAngleTemp + ((EndAngleTemp + 2 * Math.PI) - StartAngleTemp) / 2;
                }
                else
                {
                    MidAngleTemp = StartAngleTemp + (EndAngleTemp - StartAngleTemp) / 2;
                }
                newItem.PosMid.X = curItem.Center.X + curItem.Radius * Math.Cos(MidAngleTemp);
                newItem.PosMid.Y = curItem.Center.Y + curItem.Radius * Math.Sin(MidAngleTemp);
                newItem.PosEnd.X = curItem.Center.X + curItem.Radius * Math.Cos(EndAngleTemp);
                newItem.PosEnd.Y = curItem.Center.Y + curItem.Radius * Math.Sin(EndAngleTemp);
            }
            //double dStartAngleTemp=Math.Atan2(CenterY)
            return;
        }
        public void CaliStartPtAndNormal(GluePathItemCircle newItem, netDxf.Entities.Circle curItem)
        {
          
            newItem.Center.X = curItem.Center.X;
            newItem.Center.Y = curItem.Center.Y;
            if (curItem.Normal.IsNormalized == false)
                newItem.圆的角度 = -360.0;
            newItem.R = curItem.Radius;
            newItem.Start.X = curItem.Center.X+ curItem.Radius;
            newItem.Start.Y = curItem.Center.Y;
            return;
        }
        public void DrawPathUnit(DrawerControl drawerControl)
        {
            if (bInitOk)
            {
                foreach (GluePathItemBase item in gluePathUnit.listContinueItems)
                {
                    item.DrawItem(drawerControl,DrawMode.Background);
                }
            }

        }
       
    }
}
